package cn.baiweigang.uu.api.swap;

import cn.baiweigang.uu.api.BaseApi;
import cn.baiweigang.uu.enums.ContractCodeEnum;
import cn.baiweigang.uu.enums.DirectionEnum;
import cn.baiweigang.uu.enums.SymbolEnum;
import cn.baiweigang.uu.model.AccountInfo;
import cn.baiweigang.uu.model.PositionInfo;
import cn.baiweigang.uu.utils.LogUtil;
import com.huobi.swap.api.response.account.SwapAccountInfoResponse;
import com.huobi.swap.api.response.account.SwapPositionInfoResponse;
import com.huobi.swap.api.service.account.AccountAPIServiceImpl;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AccountSwapApi extends BaseApi {
    AccountAPIServiceImpl accountAPIService;

    public void updateAccountInfo(AccountInfo accountInfo) {
        try {
            init(accountInfo);
            SwapAccountInfoResponse response = accountAPIService.getSwapAccountInfo(accountInfo.getContractCode().getCode());
            if (response != null && response.getData() != null && response.getData().size() > 0) {
                SwapAccountInfoResponse.DataBean dataBean = response.getData().get(0);
                accountInfo.setContractCode(ContractCodeEnum.byCode(dataBean.getContractCode()));
                accountInfo.setLeverRate(dataBean.getLever_rate().intValue());
                accountInfo.setLiquidationPrice(dataBean.getLiquidationPrice());
                accountInfo.setMarginAvailable(dataBean.getMarginAvailable());
                accountInfo.setMarginBalance(dataBean.getMarginBalance());
                accountInfo.setMarginFrozen(dataBean.getMarginFrozen());
                accountInfo.setMarginPosition(dataBean.getMarginPosition());
                accountInfo.setMarginStatic(dataBean.getMarginStatic());
                accountInfo.setRiskRate(dataBean.getRiskRate());
                accountInfo.setSymbol(SymbolEnum.valueOf(dataBean.getSymbol()));
                accountInfo.setWithdrawAvailable(dataBean.getWithdrawAvailable());
            }
        } catch (Throwable e) {
            LogUtil.logError("查询账户信息出错 " + e.getMessage());
        }
    }

    public void updatePositionInfo(AccountInfo accountInfo) {
        try {
            init(accountInfo);
            SwapPositionInfoResponse response = accountAPIService.getSwapPositionInfo(accountInfo.getContractCode().getCode());
            if (response != null && response.getData() != null && response.getData().size() > 0) {
                List<PositionInfo> positionInfoList = new ArrayList<>();
                for (SwapPositionInfoResponse.DataBean dataBean : response.getData()) {
                    PositionInfo positionInfo = new PositionInfo();
                    positionInfo.setAvailable((int) dataBean.getAvailable().longValue());
                    positionInfo.setContractCode(ContractCodeEnum.byCode(dataBean.getContractCode()));
                    positionInfo.setCostHold(dataBean.getCostHold());
                    positionInfo.setCostOpen(dataBean.getCostOpen());
                    positionInfo.setDirection(DirectionEnum.byCode(dataBean.getDirection()));
                    positionInfo.setFrozen((int) dataBean.getFrozen().longValue());
                    positionInfo.setLastPrice(dataBean.getLastPrice());
                    positionInfo.setPositionMargin(dataBean.getPositionMargin());
                    positionInfo.setProfit(dataBean.getProfit());
                    positionInfo.setProfitRate(dataBean.getProfitRate());
                    positionInfo.setProfitUnreal(dataBean.getProfitUnreal());
                    positionInfo.setSymbol(SymbolEnum.valueOf(dataBean.getSymbol()));
                    positionInfo.setVolume((int) dataBean.getVolume().longValue());

                    positionInfoList.add(positionInfo);
                }
                accountInfo.setPositionInfo(positionInfoList);
            }
        } catch (Throwable e) {
            LogUtil.logError("查询账户信息出错 " + e.getMessage());
        }
    }

    private void init(AccountInfo accountInfo) {
        if (accountAPIService == null) {
            loadByAccountName(accountInfo.getAccountName());
            accountAPIService = new AccountAPIServiceImpl(apiKeyInfo.getApiKey(), apiKeyInfo.getSecretKey());
        }
    }
}
